part of '../controller.dart';

const _kOfferRedeemedNotificationTypeValue = 'OFFER_REDEEMED';

class _OfferRedeemedNotificationDto extends Equatable implements _NotificationDto {
  const _OfferRedeemedNotificationDto._internal({
    required this.transactionDto,
    required this.renewalInfoDto,
  });

  @override
  final AppStoreApiTransactionDto transactionDto;

  @override
  final AppStoreApiPurchasedSubscriptionRenewalInfoDto renewalInfoDto;

  @override
  List<Object?> get props {
    return [
      transactionDto,
      renewalInfoDto,
    ];
  }

  factory _OfferRedeemedNotificationDto.fromJson(Map<String, dynamic> json) {
    final dataJson = json[_kNotificationDataKey] as Map<String, dynamic>;

    final transactionDto =
        AppStoreApiTransactionDtoFactory().fromJws(dataJson[_kNotificationTransactionKey]);

    final renewalInfoDto = AppStoreApiPurchasedSubscriptionRenewalInfoDto.fromJws(
        dataJson[_kNotificationRenewalInfoKey]);

    return _OfferRedeemedNotificationDto._internal(
      transactionDto: transactionDto,
      renewalInfoDto: renewalInfoDto,
    );
  }

  @override
  OfferRedeemedAppStoreApiNotification toEntity() {
    return OfferRedeemedAppStoreApiNotification(
      transaction: transactionDto.toEntity(),
      renewalInfo: renewalInfoDto.toEntity(),
    );
  }
}
